package ga;

import entity.Poem;
import entity.Word;
import util.JsonUtil4Poem;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author: ChenforCode
 * @date: 2018/12/29 16:44
 * @description: 计算两个词的相关度
 */
public class AssociationDegree {
    public static int getAssociationDegree(Word w1, Word w2) {
        int associationDegree = 0;
        JsonUtil4Poem util = new JsonUtil4Poem();
        List<Poem> poems = new ArrayList<Poem>();
        //得到所有诗的集合
        for (int i = 0; i < 58; i++) {
            String filePath = "E:\\LiuChen\\IDEA Java\\GAPoemGenerator\\corpus\\simplified\\jueju\\" + i + ".json";
            poems.addAll(util.parse(filePath));
        }

        Iterator it = poems.iterator();
        while (it.hasNext()) {
            Poem tempPoem = (Poem) it.next();
            List<String> paras = tempPoem.getParagraphs();
            String strParas = paras.get(0);
            if (paras.size() == 2) {
                strParas += paras.get(1);
            }
            if (strParas.contains(w1.getWord()) && strParas.contains(w2.getWord())) {
                associationDegree++;
            }
        }
        return associationDegree;
    }
}
